package fenyu.personblob.web.controller;

import fenyu.personblob.single.SinglePathDoor;
import fenyu.personblob.web.model.*;
import fenyu.personblob.web.service.BlobService;
import fenyu.personblob.web.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * create at 2018/12/22 8:42
 *
 * @author 纷羽 <gzj0078@gmail.com>
 */
@Controller
@RequestMapping("/api")
public class MainController {

    @Autowired
    private UserService userService;
    @Autowired
    private BlobService blobService;

    @GetMapping("/user/register")
    @ResponseBody
    public ApiReponse<UserShow> register(String username, String password, String nickname) {
        try {
            return userService.register(username, password, nickname);
        } catch (Exception e) {
            SinglePathDoor.getSingleSave().getSaveDoor().createlog(3, "未知异常" + e.getLocalizedMessage());
            return new ApiReponse<>(500, "未知异常");
        }
    }

    @GetMapping("/user/trytoken")
    @ResponseBody
    public ApiReponse<UserShow> trytoken(String token) {
        try {
            return userService.trytoken(token);
        } catch (Exception e) {
            SinglePathDoor.getSingleSave().getSaveDoor().createlog(3, "未知异常" + e.getLocalizedMessage());
            return new ApiReponse<>(500, "未知异常");
        }
    }

    @GetMapping("/user/login")
    @ResponseBody
    public ApiReponse<LoginData> login(String username, String password) {
        try {
            return userService.login(username, password);
        } catch (Exception e) {
            SinglePathDoor.getSingleSave().getSaveDoor().createlog(3, "未知异常" + e.getLocalizedMessage());
            return new ApiReponse<>(500, "未知异常");
        }
    }

    @GetMapping("/user/gets")
    @ResponseBody
    public ApiReponse<List<UserShow>> getuserlist(String token) {
        try {
            return userService.getuserlist(token);
        } catch (Exception e) {
            SinglePathDoor.getSingleSave().getSaveDoor().createlog(3, "未知异常" + e.getLocalizedMessage());
            return new ApiReponse<>(500, "未知异常");
        }
    }

    @GetMapping("/user/changepower")
    @ResponseBody
    public ApiReponse changePower(String token, String type, Integer aim) {
        try {
            return userService.changePower(token, type, aim);
        } catch (Exception e) {
            SinglePathDoor.getSingleSave().getSaveDoor().createlog(3, "未知异常" + e.getLocalizedMessage());
            return new ApiReponse<>(500, "未知异常");
        }
    }

    @GetMapping("/blob/list")
    @ResponseBody
    public ApiReponse<List<BlobShowSimple>> bloblist() {
        try {
            return blobService.bloblist();
        } catch (Exception e) {
            SinglePathDoor.getSingleSave().getSaveDoor().createlog(3, "未知异常" + e.getLocalizedMessage());
            return new ApiReponse<>(500, "未知异常");
        }
    }

    @GetMapping("/blob")
    @ResponseBody
    public ApiReponse<BlobShow> singleblob(Integer blob_id) {
        try {
            return blobService.singleblob(blob_id);
        } catch (Exception e) {
            SinglePathDoor.getSingleSave().getSaveDoor().createlog(3, "未知异常" + e.getLocalizedMessage());
            return new ApiReponse<>(500, "未知异常");
        }
    }

    @PostMapping("/blob/send")
    @ResponseBody
    public ApiReponse<String> sendBlob(String token, String tittle, String text) {
        try {
            return blobService.sendBlob(token, tittle, text);
        } catch (Exception e) {
            SinglePathDoor.getSingleSave().getSaveDoor().createlog(3, "未知异常" + e.getLocalizedMessage());
            return new ApiReponse<>(500, "未知异常");
        }
    }

    @PostMapping("/blob/comment")
    @ResponseBody
    public ApiReponse<String> commentBlob(@RequestParam(value = "token") String token,
                                          @RequestParam(value = "text") String text,
                                          @RequestParam(value = "blob_id") Integer blob_id,
                                          @RequestParam(value = "comment_id", required = false) Integer comment_id) {
        try {
            return blobService.commentBlob(token, text, blob_id, comment_id);
        } catch (Exception e) {
            SinglePathDoor.getSingleSave().getSaveDoor().createlog(3, "未知异常" + e.getLocalizedMessage());
            return new ApiReponse<>(500, "未知异常");
        }
    }

    @GetMapping("admin/print")
    @ResponseBody
    public ApiReponse printLog(String token) {
        return userService.printLog(token);
    }
}
